/*   Copyright 2012 Fernando Moreno Ruiz All Rights Reserved.
 *
 * 	 This file is part of pyPhotosMixer.
 *
 *   pyPhotosMixer is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   pyPhotosMixer is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with pyPhotosMixer.  If not, see <http://www.gnu.org/licenses/>.
 * */
import QtQuick 1.1
import "../date"
import "../javascript/Date.js" as JsDate
Rectangle {
    id: datePhotos
    width: parent.width
    height: displayed ? 120 : 30
    property string folderDir: ""
    property string maxDate:"";
    property string minDate:""
    property alias model:repDatePhotos.model;
    property variant listView;
    property color colorDelegate: "red"
    property bool down: false
    property bool displayed:false
    property bool ferFormat: false
    MouseArea{
        anchors.fill: parent
        onClicked: {
            displayed = !displayed
        }
    }

    Repeater{
        id:repDatePhotos
        delegate:Rectangle{
            id:delegatePhoto
            height:parent.height
            color:"#00000000"
            width: displayed?imageP.width:0
            Rectangle{
                anchors.left: parent.left
                height:parent.height
                z:(index==listView.selectedIndex)?2:1
                width:(index==listView.selectedIndex)?1:1
                color:(index==listView.selectedIndex)?"blue":colorDelegate

            }
            Image{
                id:imageP
                source:thumbail
                fillMode:Image.PreserveAspectFit
                width: 100
                height: displayed?100:0
                Behavior on height{
                    NumberAnimation { duration: 600 }
                }
            }
            MouseArea{
                anchors.fill:imageP
                hoverEnabled: true
                onEntered: {
                    var component = Qt.createComponent("InfoPhoto.qml")
                    imagePhoto = component.createObject(delegatePhoto)
                    imagePhoto.width = 300
                    imagePhoto.height = 300
                    imagePhoto.source = thumbail
                    if(!down){
                        imagePhoto.anchors.bottom = parent.top
                    }else{
                        imagePhoto.anchors.top = parent.bottom
                    }
                    imagePhoto.fillMode = Image.PreserveAspectFit
                }
                onExited: {
                    if(!imagePhoto.stay)
                        imagePhoto.destroy(100)
                }
                onClicked: {

                    listView.currentIndex = indexPicture
                    listView.selectedIndex = indexPicture
                    imagePhoto.stay = true
                }
            }
            MouseArea{
                id:mouseAreaThumbail
                hoverEnabled: true
                anchors.fill:parent
                property variant imagePhoto
                onEntered: {
                    var component = Qt.createComponent("InfoPhoto.qml")
                    imagePhoto = component.createObject(delegatePhoto)
                    imagePhoto.width = 300
                    imagePhoto.height = 300
                    imagePhoto.source = thumbail
                    if(!down){
                        imagePhoto.anchors.bottom = parent.top
                    }else{
                        imagePhoto.anchors.top = parent.bottom
                    }
                    imagePhoto.fillMode = Image.PreserveAspectFit
                }
                onClicked: {
                    console.log("INDEX;"+index)
                    listView.currentIndex = indexPicture
                    listView.selectedIndex = indexPicture
                    imagePhoto.stay = true
                }
                onExited: {
                    if(!imagePhoto.stay)
                        imagePhoto.destroy(100)
                }
            }
            FerFormat{
                id:conv_ff
                file: fileName
            }
            Component.onCompleted:{

                entra("COMPLETED:" + folderDir)
                if(maxDate!="" && minDate != ""){
                    var pp  = JsDate.stringToDate(maxDate) - JsDate.stringToDate(minDate)
                    console.log("ferformat:"+ferFormat)
                    if (ferFormat===false){
                        console.log("EX:" + exif)
                        var percentage = (JsDate.stringToDate(exif)-JsDate.stringToDate(minDate))/pp
                        console.log("PERCENTAGE:"+ percentage)
                    }else{
                        console.log("dateshow:"+conv_ff.dateShow)
                        percentage = (JsDate.stringToDate(conv_ff.dateShow)-JsDate.stringToDate(minDate))/pp
                    }

                    delegatePhoto.x = percentage * datePhotos.width-1
                }else{
                    visible = false
                }
            }
        }
        onCountChanged: {
            maxDate = ""
            minDate = ""
        }
    }

//    onMaxDateChanged: {
//        if(maxDate!="" && minDate != ""){
//            repDatePhotos.model = datePhotos.model
//        }
//    }
//    onMinDateChanged: {
//        if(maxDate!="" && minDate != ""){
//            repDatePhotos.model = datePhotos.model
//        }
//    }
    Behavior on height {
        NumberAnimation{duration:600}
    }
    Behavior on anchors.verticalCenterOffset{
        NumberAnimation{
            duration:600
        }
    }
}
